package com.xinongbest.mall.common;

import com.google.common.collect.Sets;
import com.xinongbest.mall.exception.ImoocMallException;
import com.xinongbest.mall.exception.ImoocMallExceptionEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @lemonwan 常量
 */
@Component
public class Constant {
    /**
     * 密码加盐字符串常量
     */
    public static final String SALT = "@#$%java*&";

    /**
     * 密码加盐数字：字符运算
     */
    public static final Integer SALT_NUM = 1999;

    /**
     * 登录用户信息保存在session中的键
     */
    public static final String IMOOC_MALL_USER = "imooc_mall_user";

    /**
     * 上传文件的路径
     */
    public static String FILE_UPLOAD_DIR;

    @Value("${file.upload.dir}")
    public void setFileUploadDir(String fileUploadDir) {
        FILE_UPLOAD_DIR = fileUploadDir;
    }

    public interface ProductListOrderBy {
        Set<String> PRICE_DESC_ASC = Sets.newHashSet("price desc", "price asc");
    }

    /**
     * 商品上架状态
     */
    public interface SaleStatus {
        /**
         * 商品下架状态
         */
        int NOT_SALE = 0;

        /**
         * 商品上架状态
         */
        int SALE = 1;
    }

    /**
     * 购物车商品选中状态
     */
    public interface CartProductStatus {
        /**
         * 选中
         */
        int CHECKED = 1;
        /**
         * 未选中
         */
        int NOt_CHECKED = 0;
    }

    public enum OrderStatusEnum {
        CANCELED(0, "用户已取消"),
        NOT_PAID(10, "未付款"),
        PAID(20, "已付款"),
        DELIVERED(30, "已发货"),
        FINISHED(40, "交易完成");

        private String value;
        private int code;

        OrderStatusEnum(int code, String value) {
            this.value = value;
            this.code = code;
        }

        public static OrderStatusEnum codeOf(int code) {
            for (OrderStatusEnum orderStatusEnum : values()) {
                if (orderStatusEnum.getCode() == code) {
                    return orderStatusEnum;
                }
            }
            throw new ImoocMallException(ImoocMallExceptionEnum.NO_ENUM);
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}
